{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "from math import sin, cos, pi, log\n",
    "from tkinter import *\n",
    "\n",
    "CANVAS_WIDTH = 640  # 画布的宽\n",
    "CANVAS_HEIGHT = 480  # 画布的高\n",
    "CANVAS_CENTER_X = CANVAS_WIDTH / 2  # 画布中心的X轴坐标\n",
    "CANVAS_CENTER_Y = CANVAS_HEIGHT / 2  # 画布中心的Y轴坐标\n",
    "IMAGE_ENLARGE = 11  # 放大比例\n",
    "HEART_COLOR = \"#ff2121\"  # 心的颜色，这个是中国红\n",
    "\n",
    "\n",
    "def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):\n",
    "    \"\"\"\n",
    "    “爱心函数生成器”\n",
    "    :param shrink_ratio: 放大比例\n",
    "    :param t: 参数\n",
    "    :return: 坐标\n",
    "    \"\"\"\n",
    "    # 基础函数\n",
    "    x = 16 * (sin(t) ** 3)\n",
    "    y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))\n",
    "\n",
    "    # 放大\n",
    "    x *= shrink_ratio\n",
    "    y *= shrink_ratio\n",
    "\n",
    "    # 移到画布中央\n",
    "    x += CANVAS_CENTER_X\n",
    "    y += CANVAS_CENTER_Y\n",
    "\n",
    "    return int(x), int(y)\n",
    "\n",
    "\n",
    "def scatter_inside(x, y, beta=0.15):\n",
    "    \"\"\"\n",
    "    随机内部扩散\n",
    "    :param x: 原x\n",
    "    :param y: 原y\n",
    "    :param beta: 强度\n",
    "    :return: 新坐标\n",
    "    \"\"\"\n",
    "    ratio_x = - beta * log(random.random())\n",
    "    ratio_y = - beta * log(random.random())\n",
    "\n",
    "    dx = ratio_x * (x - CANVAS_CENTER_X)\n",
    "    dy = ratio_y * (y - CANVAS_CENTER_Y)\n",
    "\n",
    "    return x - dx, y - dy\n",
    "\n",
    "\n",
    "def shrink(x, y, ratio):\n",
    "    \"\"\"\n",
    "    抖动\n",
    "    :param x: 原x\n",
    "    :param y: 原y\n",
    "    :param ratio: 比例\n",
    "    :return: 新坐标\n",
    "    \"\"\"\n",
    "    force = -1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.6)  # 这个参数...\n",
    "    dx = ratio * force * (x - CANVAS_CENTER_X)\n",
    "    dy = ratio * force * (y - CANVAS_CENTER_Y)\n",
    "    return x - dx, y - dy\n",
    "\n",
    "\n",
    "def curve(p):\n",
    "    \"\"\"\n",
    "    自定义曲线函数，调整跳动周期\n",
    "    :param p: 参数\n",
    "    :return: 正弦\n",
    "    \"\"\"\n",
    "    # 可以尝试换其他的动态函数，达到更有力量的效果（贝塞尔？）\n",
    "    return 2 * (2 * sin(4 * p)) / (2 * pi)\n",
    "\n",
    "\n",
    "class Heart:\n",
    "    \"\"\"\n",
    "    爱心类\n",
    "    \"\"\"\n",
    "\n",
    "    def __init__(self, generate_frame=20):\n",
    "        self._points = set()  # 原始爱心坐标集合\n",
    "        self._edge_diffusion_points = set()  # 边缘扩散效果点坐标集合\n",
    "        self._center_diffusion_points = set()  # 中心扩散效果点坐标集合\n",
    "        self.all_points = {}  # 每帧动态点坐标\n",
    "        self.build(2000)\n",
    "\n",
    "        self.random_halo = 1000\n",
    "\n",
    "        self.generate_frame = generate_frame\n",
    "        for frame in range(generate_frame):\n",
    "            self.calc(frame)\n",
    "\n",
    "    def build(self, number):\n",
    "        # 爱心\n",
    "        for _ in range(number):\n",
    "            t = random.uniform(0, 2 * pi)  # 随机不到的地方造成爱心有缺口\n",
    "            x, y = heart_function(t)\n",
    "            self._points.add((x, y))\n",
    "\n",
    "        # 爱心内扩散\n",
    "        for _x, _y in list(self._points):\n",
    "            for _ in range(3):\n",
    "                x, y = scatter_inside(_x, _y, 0.05)\n",
    "                self._edge_diffusion_points.add((x, y))\n",
    "\n",
    "        # 爱心内再次扩散\n",
    "        point_list = list(self._points)\n",
    "        for _ in range(4000):\n",
    "            x, y = random.choice(point_list)\n",
    "            x, y = scatter_inside(x, y, 0.17)\n",
    "            self._center_diffusion_points.add((x, y))\n",
    "\n",
    "    @staticmethod\n",
    "    def calc_position(x, y, ratio):\n",
    "        # 调整缩放比例\n",
    "        force = 1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.520)  # 魔法参数\n",
    "\n",
    "        dx = ratio * force * (x - CANVAS_CENTER_X) + random.randint(-1, 1)\n",
    "        dy = ratio * force * (y - CANVAS_CENTER_Y) + random.randint(-1, 1)\n",
    "\n",
    "        return x - dx, y - dy\n",
    "\n",
    "    def calc(self, generate_frame):\n",
    "        ratio = 10 * curve(generate_frame / 10 * pi)  # 圆滑的周期的缩放比例\n",
    "\n",
    "        halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi)))\n",
    "        halo_number = int(3000 + 4000 * abs(curve(generate_frame / 10 * pi) ** 2))\n",
    "\n",
    "        all_points = []\n",
    "\n",
    "        # 光环\n",
    "        heart_halo_point = set()  # 光环的点坐标集合\n",
    "        for _ in range(halo_number):\n",
    "            t = random.uniform(0, 2 * pi)  # 随机不到的地方造成爱心有缺口\n",
    "            x, y = heart_function(t, shrink_ratio=11.6)  # 魔法参数\n",
    "            x, y = shrink(x, y, halo_radius)\n",
    "            if (x, y) not in heart_halo_point:\n",
    "                # 处理新的点\n",
    "                heart_halo_point.add((x, y))\n",
    "                x += random.randint(-14, 14)\n",
    "                y += random.randint(-14, 14)\n",
    "                size = random.choice((1, 2, 2))\n",
    "                all_points.append((x, y, size))\n",
    "\n",
    "        # 轮廓\n",
    "        for x, y in self._points:\n",
    "            x, y = self.calc_position(x, y, ratio)\n",
    "            size = random.randint(1, 3)\n",
    "            all_points.append((x, y, size))\n",
    "\n",
    "        # 内容\n",
    "        for x, y in self._edge_diffusion_points:\n",
    "            x, y = self.calc_position(x, y, ratio)\n",
    "            size = random.randint(1, 2)\n",
    "            all_points.append((x, y, size))\n",
    "\n",
    "        for x, y in self._center_diffusion_points:\n",
    "            x, y = self.calc_position(x, y, ratio)\n",
    "            size = random.randint(1, 2)\n",
    "            all_points.append((x, y, size))\n",
    "\n",
    "        self.all_points[generate_frame] = all_points\n",
    "\n",
    "    def render(self, render_canvas, render_frame):\n",
    "        for x, y, size in self.all_points[render_frame % self.generate_frame]:\n",
    "            render_canvas.create_rectangle(x, y, x + size, y + size, width=0, fill=HEART_COLOR)\n",
    "\n",
    "\n",
    "def draw(main: Tk, render_canvas: Canvas, render_heart: Heart, render_frame=0):\n",
    "    render_canvas.delete('all')\n",
    "    render_heart.render(render_canvas, render_frame)\n",
    "    main.after(160, draw, main, render_canvas, render_heart, render_frame + 1)\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    root = Tk()  # 一个Tk\n",
    "    canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)\n",
    "    canvas.pack()\n",
    "    heart = Heart()  # 心\n",
    "    draw(root, canvas, heart)  # 开始画画~\n",
    "    root.mainloop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
